package com.njtoyo.taxi.admin.logic.business.ride;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.njtoyo.taxi.admin.logic.business.TaxisLogic;
import com.njtoyo.taxi.admin.mapper.master.entity.Ride;
import com.njtoyo.taxi.admin.mapper.master.entity.RideCancellation;
import com.njtoyo.taxi.admin.mapper.master.entity.RideOrder;
import com.njtoyo.taxi.admin.mapper.master.entity.RideState;
import com.njtoyo.taxi.admin.mapper.master.enums.RideOrderPaidThrough;
import com.njtoyo.taxi.admin.mapper.master.enums.RideOrderState;
import com.njtoyo.taxi.admin.mapper.master.enums.RideStateState;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideCancellationMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideOrderMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.RideStateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class RideLogic {

    @Autowired
    private RideMapper rideMapper;

    @Autowired
    private RideOrderLogic rideOrderLogic;

    @Autowired
    private RideCancellationLogic rideCancellationLogic;

    @Autowired
    private TaxisLogic taxisLogic;

    @Autowired
    private RideOrderMapper rideOrderMapper;

    @Autowired
    private RideStateMapper rideStateMapper;

    @Autowired
    private RideCancellationMapper rideCancellationMapper;

    /**
     * 有责改无责，操作Ride.state
     *
     * @return
     */
    public boolean cancellationResponsibilityEliminate(Long rideId) {
        return updateState(rideId, RideStateState.cancelled);
    }

    /**
     * 修改指定ride的state
     */
    public boolean updateState(Long rideId, RideStateState state) {
        return new LambdaUpdateChainWrapper<>(rideMapper)
                .eq(Ride::getId, rideId)
                .set(Ride::getState, state)
                .set(Ride::getUpdatedAt, new Date())
                .update();
    }

    // 判断该行程是否存在已付款的订单
    public boolean hasPaidOrder(Long rideId) {
        return Objects.nonNull(getPaidOrderByRideId(rideId));
    }

    // 判断该行程是否存在已付款的订单
    public RideOrder getPaidOrderByRideId(Long rideId) {
        RideOrder rideOrder = rideOrderLogic.getOneRideOrderByRideId(rideId);

        if (Objects.nonNull(rideOrder) &&
                RideOrderPaidThrough.online.equals(rideOrder.getPresetThrough()) &&
                RideOrderState.done.equals(rideOrder.getState())) {

            return rideOrder;
        }

        return null;
    }

    @Transactional
    // 强制取消+管理员直接取消行程
    public boolean cancel(Ride ride) {
        // 1. 修改ride状态
        boolean updateRideState = new LambdaUpdateChainWrapper<>(rideMapper)
                .eq(Ride::getId, ride.getId())
                .set(Ride::getState, RideStateState.cancelled)
                .set(Ride::getUpdatedAt, new Date())
                .update();

        // 2. 新增 RideCancellation 记录
        boolean insertRideCancellation = rideCancellationLogic.insertRecordForCancel(ride);

        // 3. 更新车辆信息
        if (Objects.nonNull(ride.getTaxiId())) {
            taxisLogic.updateIsAvailableTrue(ride.getTaxiId());
        }

        // 4. 判断行程是否有未付款的订单
        RideOrder unpaidRideOrder = rideOrderLogic.getUnpaidOrderByRideId(ride.getId());
        if (Objects.nonNull(unpaidRideOrder)) {
            // 有订单，强制取消
            rideOrderMapper.deleteById(unpaidRideOrder.getId());
        }

        return updateRideState && insertRideCancellation;
    }

    @Transactional
    public boolean updateStateTrans(Long rideId, RideStateState state) {
        // 状态2改3、4|状态3改4
        //数据库操作两张表更新
        boolean updateRideState = new LambdaUpdateChainWrapper<>(rideMapper)
                .eq(Ride::getId, rideId)
                .set(Ride::getState, state)
                .set(Ride::getUpdatedAt, new Date())
                .update();

        RideState rideStateInsert = new RideState();
        rideStateInsert.setRideId(rideId);
        rideStateInsert.setState(state);
        rideStateInsert.setCreatedAt(new Date());

        boolean insert = rideStateMapper.insert(rideStateInsert) > 0;

        return updateRideState && insert;
    }

    // 获取该行程是否存在乘客的有责取消行为
    public RideCancellation getCustomerForceCancellation(Long rideId) {
        List<RideCancellation> list = new LambdaQueryChainWrapper<>(rideCancellationMapper)
                .select(RideCancellation::getId, RideCancellation::getViolate, RideCancellation::getResponsibility)
                .eq(RideCancellation::getRideId, rideId)
                .orderByDesc(RideCancellation::getId)
                .list();

        if (Objects.nonNull(list)) {
            Optional<RideCancellation> first = list.stream().findFirst();
            if (first.isPresent()) {
                return first.get();
            }
        }

        return null;
    }

}
